package com.boho.ecp.cart.action;

import java.sql.Connection;
import java.util.List;

import cn.boho.framework.actions.ActionImp;
import cn.boho.framework.context.ActionContext;
import cn.boho.framework.service.MessageService;

import com.boho.ecp.base.model.Product;
import com.boho.ecp.cart.service.CartService;
import com.boho.ecp.util.JsonUtils;
import com.google.gson.reflect.TypeToken;

public class UpdateProductToCartAction extends ActionImp {

	private String product;
	
	@Override
	protected void doException(ActionContext atx, Exception ex) {
		atx.setErrorContext("UPDATE_PRODUCT_FAIL", MessageService.getMessage("UPDATE_PRODUCT_FAIL"), ex);
	}

	@Override
	protected int performExecute(ActionContext atx) throws Exception {
		Connection con = atx.getConection();
		List<Product> list = JsonUtils.fromJson(product, new TypeToken<List<Product>>(){}.getType());
		CartService.saveCart(atx, con, list);
		return 1;
	}

	@Override
	protected int verifyParameters(ActionContext atx) throws Exception {
		product = atx.getStringValue("PRODUCT");
		return 1;
	}
}
